{
 "cells": [
  {
   "metadata": {},
   "cell_type": "markdown",
   "source": "# 6 Pandas函数应用",
   "id": "87f0b2547396dfe3"
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-01-08T08:59:45.829474Z",
     "start_time": "2025-01-08T08:59:45.822671Z"
    }
   },
   "cell_type": "code",
   "source": [
    "import pandas as pd\n",
    "import numpy as np\n",
    "\n",
    "df = pd.DataFrame(np.random.randn(5, 4) - 1)\n",
    "\n",
    "print(df)\n",
    "print(\"-\" * 50)\n",
    "\n",
    "print(df.abs())  # 取绝对值\n"
   ],
   "id": "66505bcda6c377fa",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "          0         1         2         3\n",
      "0 -2.509073 -1.538458 -1.526595 -0.266890\n",
      "1 -0.173878 -0.474567 -2.049729 -1.016482\n",
      "2 -0.311497 -0.582962 -0.983753 -0.493576\n",
      "3 -0.749951  0.538843 -0.244727 -1.106087\n",
      "4  0.494913 -0.848227  0.112155 -1.955705\n",
      "--------------------------------------------------\n",
      "          0         1         2         3\n",
      "0  2.509073  1.538458  1.526595  0.266890\n",
      "1  0.173878  0.474567  2.049729  1.016482\n",
      "2  0.311497  0.582962  0.983753  0.493576\n",
      "3  0.749951  0.538843  0.244727  1.106087\n",
      "4  0.494913  0.848227  0.112155  1.955705\n"
     ]
    }
   ],
   "execution_count": 4
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-01-08T09:02:36.732924Z",
     "start_time": "2025-01-08T09:02:36.726820Z"
    }
   },
   "cell_type": "code",
   "source": [
    "# apply默认作用在列上,x是每一列,因为axis=0\n",
    "print(df.apply(lambda x: x.max()))\n",
    "# 0    0.494913\n",
    "# 1    0.538843\n",
    "# 2    0.112155\n",
    "# 3   -0.266890\n",
    "# dtype: float64\n",
    "print(\"-\" * 50)\n",
    "\n",
    "# apply作用在行上，需要指定axis=1\n",
    "print(df.apply(lambda x: x.max(), axis=1))\n",
    "# 0   -0.266890\n",
    "# 1   -0.173878\n",
    "# 2   -0.311497\n",
    "# 3    0.538843\n",
    "# 4    0.494913\n",
    "# dtype: float64"
   ],
   "id": "935da00aa4167455",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "0    0.494913\n",
      "1    0.538843\n",
      "2    0.112155\n",
      "3   -0.266890\n",
      "dtype: float64\n",
      "--------------------------------------------------\n",
      "0   -0.266890\n",
      "1   -0.173878\n",
      "2   -0.311497\n",
      "3    0.538843\n",
      "4    0.494913\n",
      "dtype: float64\n"
     ]
    }
   ],
   "execution_count": 6
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-01-08T09:05:32.198002Z",
     "start_time": "2025-01-08T09:05:32.192496Z"
    }
   },
   "cell_type": "code",
   "source": [
    "# map应用到每个数据上\n",
    "print(df.map(lambda x: \"%.2f\" % x))"
   ],
   "id": "1ba37567f6d93bcb",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "       0      1      2      3\n",
      "0  -2.51  -1.54  -1.53  -0.27\n",
      "1  -0.17  -0.47  -2.05  -1.02\n",
      "2  -0.31  -0.58  -0.98  -0.49\n",
      "3  -0.75   0.54  -0.24  -1.11\n",
      "4   0.49  -0.85   0.11  -1.96\n"
     ]
    }
   ],
   "execution_count": 7
  },
  {
   "metadata": {},
   "cell_type": "markdown",
   "source": "# 6.4 索引排序（不太重要）",
   "id": "9bf5ae69ea2c622b"
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-01-08T09:15:31.518011Z",
     "start_time": "2025-01-08T09:15:31.511194Z"
    }
   },
   "cell_type": "code",
   "source": [
    "# Series\n",
    "\n",
    "print(np.random.randint(5, size=5))  # 随机生成5个整数\n",
    "print(\"-\" * 50)\n",
    "\n",
    "# 索引随机生成\n",
    "s4 = pd.Series(range(10, 15), index=np.random.randint(5, size=5))\n",
    "print(s4)\n",
    "print(\"-\" * 50)\n",
    "\n",
    "# 索引排序,sort_index返回一个新的排好索引的series\n",
    "print(s4.sort_index())  # sort_index不改变原来的series\n",
    "print(\"-\" * 50)\n",
    "print(s4)\n",
    "print(\"-\" * 50)\n",
    "\n",
    "print(s4.iloc[0:3])\n",
    "print(\"-\" * 50)\n",
    "\n",
    "# print(s4.loc[0:3]) # loc索引值不唯一时直接报错"
   ],
   "id": "220e1f96de32feb",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[1 0 4 1 2]\n",
      "--------------------------------------------------\n",
      "3    10\n",
      "0    11\n",
      "3    12\n",
      "1    13\n",
      "0    14\n",
      "dtype: int64\n",
      "--------------------------------------------------\n",
      "0    11\n",
      "0    14\n",
      "1    13\n",
      "3    10\n",
      "3    12\n",
      "dtype: int64\n",
      "--------------------------------------------------\n",
      "3    10\n",
      "0    11\n",
      "3    12\n",
      "1    13\n",
      "0    14\n",
      "dtype: int64\n",
      "--------------------------------------------------\n",
      "3    10\n",
      "0    11\n",
      "3    12\n",
      "dtype: int64\n",
      "--------------------------------------------------\n"
     ]
    }
   ],
   "execution_count": 16
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-01-08T09:24:55.911810Z",
     "start_time": "2025-01-08T09:24:55.899960Z"
    }
   },
   "cell_type": "code",
   "source": [
    "# DataFrame\n",
    "\n",
    "# 行索引和列索引随机生成\n",
    "df4 = pd.DataFrame(np.random.randn(5, 5),\n",
    "                   index=np.random.randint(5, size=5),\n",
    "                   columns=np.random.randint(5, size=5))\n",
    "\n",
    "print(df4)\n",
    "print(\"-\" * 50)\n",
    "\n",
    "# axis=0 按行索引排序\n",
    "df4_sort_row = df4.sort_index(axis=0)  # 按行索引排序\n",
    "print(df4_sort_row)\n",
    "print(\"-\" * 50)\n",
    "\n",
    "# axis=1 按列索引排序\n",
    "df4_sort_col = df4.sort_index(axis=1)  # 按列索引排序\n",
    "print(df4_sort_col)\n",
    "print(\"-\" * 50)\n"
   ],
   "id": "7fb2687971524016",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "          3         3         0         4         3\n",
      "2 -1.985255 -0.038229 -1.402192  1.639075 -0.952092\n",
      "0  0.298004 -0.443609  0.798938 -2.007611 -0.680347\n",
      "2  0.227031  1.159966 -1.154874  0.156229 -1.553057\n",
      "3  0.088594  0.501274  1.001375 -0.585464 -0.529802\n",
      "2  0.424213  0.963061  0.093829  0.202300  1.112453\n",
      "--------------------------------------------------\n",
      "          3         3         0         4         3\n",
      "0  0.298004 -0.443609  0.798938 -2.007611 -0.680347\n",
      "2 -1.985255 -0.038229 -1.402192  1.639075 -0.952092\n",
      "2  0.227031  1.159966 -1.154874  0.156229 -1.553057\n",
      "2  0.424213  0.963061  0.093829  0.202300  1.112453\n",
      "3  0.088594  0.501274  1.001375 -0.585464 -0.529802\n",
      "--------------------------------------------------\n",
      "          0         3         3         3         4\n",
      "2 -1.402192 -1.985255 -0.038229 -0.952092  1.639075\n",
      "0  0.798938  0.298004 -0.443609 -0.680347 -2.007611\n",
      "2 -1.154874  0.227031  1.159966 -1.553057  0.156229\n",
      "3  1.001375  0.088594  0.501274 -0.529802 -0.585464\n",
      "2  0.093829  0.424213  0.963061  1.112453  0.202300\n",
      "--------------------------------------------------\n"
     ]
    }
   ],
   "execution_count": 19
  },
  {
   "metadata": {},
   "cell_type": "markdown",
   "source": "# 6.5 按值排序（机器学习、深度学习不重要，数据分析才需要）",
   "id": "be7f506de8ca6ab9"
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-01-08T09:36:24.934010Z",
     "start_time": "2025-01-08T09:36:24.928613Z"
    }
   },
   "cell_type": "code",
   "source": [
    "import random\n",
    "\n",
    "tmp_list = [random.randint(1, 100) for i in range(24)]  # 随机生成24个整数\n",
    "df4 = pd.DataFrame(np.array(tmp_list).reshape(6, 4))  # 转换为6行4列的DataFrame\n",
    "print(df4)"
   ],
   "id": "ffb4b3e9f959a803",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "    0   1   2   3\n",
      "0  57   9  17  70\n",
      "1  53  43  49  59\n",
      "2  63  97  15  73\n",
      "3  52  23  64  58\n",
      "4  58  77  23  49\n",
      "5  64  62  12  34\n"
     ]
    }
   ],
   "execution_count": 20
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-01-08T09:38:19.170958Z",
     "start_time": "2025-01-08T09:38:19.166269Z"
    }
   },
   "cell_type": "code",
   "source": [
    "# 按axis=0排序，by后是列名，交换的是行向量\n",
    "df4_sort = df4.sort_values(by=3, axis=0) # 行向量根据第3列的值来排序\n",
    "print(df4_sort)"
   ],
   "id": "86156e3b70cac2c4",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "    0   1   2   3\n",
      "5  64  62  12  34\n",
      "4  58  77  23  49\n",
      "3  52  23  64  58\n",
      "1  53  43  49  59\n",
      "0  57   9  17  70\n",
      "2  63  97  15  73\n"
     ]
    }
   ],
   "execution_count": 21
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-01-08T09:41:51.418828Z",
     "start_time": "2025-01-08T09:41:51.413322Z"
    }
   },
   "cell_type": "code",
   "source": [
    "# 按axis=1排序，by后是行名，交换的是列向量\n",
    "df4_sort = df4.sort_values(by=3, axis=1) # 列向量根据第3行的值来排序\n",
    "print(df4_sort)"
   ],
   "id": "b5e1e9c4b22934b8",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "    1   0   3   2\n",
      "0   9  57  70  17\n",
      "1  43  53  59  49\n",
      "2  97  63  73  15\n",
      "3  23  52  58  64\n",
      "4  77  58  49  23\n",
      "5  62  64  34  12\n"
     ]
    }
   ],
   "execution_count": 22
  },
  {
   "metadata": {},
   "cell_type": "markdown",
   "source": "# 6.6 处理缺失数据 （重要）",
   "id": "d6b8e550577e33f3"
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-01-08T09:59:08.576614Z",
     "start_time": "2025-01-08T09:59:08.568948Z"
    }
   },
   "cell_type": "code",
   "source": [
    "df_data=pd.DataFrame([np.random.randn(3),\n",
    "                      [1., 2., np.nan],\n",
    "                      [np.nan, 4., np.nan],\n",
    "                      [1., 2., 3.]])\n",
    "print(df_data)\n",
    "print(\"-\" * 50)\n",
    "\n",
    "print(df_data.iloc[2,0])\n",
    "print(\"-\" * 50)\n",
    "\n",
    "# isnull()返回一个布尔型的DataFrame，True表示缺失值，False表示非缺失值\n",
    "print(df_data.isnull())\n",
    "print(\"-\" * 50)\n",
    "\n",
    "# 计算每列的缺失率\n",
    "print(df_data.isnull().sum()/len(df_data))"
   ],
   "id": "97f5b6d8d5c66abd",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "         0         1         2\n",
      "0  0.02715  0.912574  0.070426\n",
      "1  1.00000  2.000000       NaN\n",
      "2      NaN  4.000000       NaN\n",
      "3  1.00000  2.000000  3.000000\n",
      "--------------------------------------------------\n",
      "nan\n",
      "--------------------------------------------------\n",
      "       0      1      2\n",
      "0  False  False  False\n",
      "1  False  False   True\n",
      "2   True  False   True\n",
      "3  False  False  False\n",
      "--------------------------------------------------\n",
      "0    0.25\n",
      "1    0.00\n",
      "2    0.50\n",
      "dtype: float64\n"
     ]
    }
   ],
   "execution_count": 26
  },
  {
   "metadata": {},
   "cell_type": "markdown",
   "source": "# 删除缺失数据",
   "id": "c97110e22df68350"
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-01-08T10:29:20.562238Z",
     "start_time": "2025-01-08T10:29:20.550712Z"
    }
   },
   "cell_type": "code",
   "source": [
    "# dropna()默认删除所有缺失值，axis=0表示按行删除，axis=1表示按列删除\n",
    "# subset=['col1', 'col2']表示只删除col1和col2列的缺失值\n",
    "# inplace=True表示直接修改原数据，否则返回一个新的DataFrame\n",
    "print(df_data.dropna(axis=0)) # 行向量中有NaN，就删除该行\n",
    "print(\"-\" * 50)\n",
    "print(df_data.dropna(axis=0,subset=[0])) # 行向量中的第0列为NaN，删除该行\n",
    "print(\"-\"*50)\n",
    "print(df_data.dropna(axis=0,subset=[0,2])) # 行向量中的第0列或第1列为NaN，删除该行\n",
    "print(\"-\"*50)\n",
    "print(df_data)"
   ],
   "id": "a28518ef2ae06f20",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "         0         1         2\n",
      "0  0.02715  0.912574  0.070426\n",
      "3  1.00000  2.000000  3.000000\n",
      "--------------------------------------------------\n",
      "         0         1         2\n",
      "0  0.02715  0.912574  0.070426\n",
      "1  1.00000  2.000000       NaN\n",
      "3  1.00000  2.000000  3.000000\n",
      "--------------------------------------------------\n",
      "         0         1         2\n",
      "0  0.02715  0.912574  0.070426\n",
      "3  1.00000  2.000000  3.000000\n",
      "--------------------------------------------------\n",
      "         0         1         2\n",
      "0  0.02715  0.912574  0.070426\n",
      "1  1.00000  2.000000       NaN\n",
      "2      NaN  4.000000       NaN\n",
      "3  1.00000  2.000000  3.000000\n"
     ]
    }
   ],
   "execution_count": 32
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-01-08T10:30:39.828081Z",
     "start_time": "2025-01-08T10:30:39.818746Z"
    }
   },
   "cell_type": "code",
   "source": [
    "print(df_data.dropna(axis=1)) # 列向量中有NaN，就删除该行\n",
    "print(\"-\" * 50)\n",
    "print(df_data.dropna(axis=1,subset=[1])) # 行向量中的第0列为NaN，删除该行\n",
    "print(\"-\"*50)\n",
    "print(df_data.dropna(axis=1,subset=[1,2])) # 行向量中的第0列或第1列为NaN，删除该行\n",
    "print(\"-\"*50)\n",
    "print(df_data)"
   ],
   "id": "d89e180a1c9bb5da",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "          1\n",
      "0  0.912574\n",
      "1  2.000000\n",
      "2  4.000000\n",
      "3  2.000000\n",
      "--------------------------------------------------\n",
      "         0         1\n",
      "0  0.02715  0.912574\n",
      "1  1.00000  2.000000\n",
      "2      NaN  4.000000\n",
      "3  1.00000  2.000000\n",
      "--------------------------------------------------\n",
      "          1\n",
      "0  0.912574\n",
      "1  2.000000\n",
      "2  4.000000\n",
      "3  2.000000\n",
      "--------------------------------------------------\n",
      "         0         1         2\n",
      "0  0.02715  0.912574  0.070426\n",
      "1  1.00000  2.000000       NaN\n",
      "2      NaN  4.000000       NaN\n",
      "3  1.00000  2.000000  3.000000\n"
     ]
    }
   ],
   "execution_count": 34
  },
  {
   "metadata": {},
   "cell_type": "markdown",
   "source": "# 填充缺失数据",
   "id": "3f109a046a856dfd"
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-01-08T10:36:30.540606Z",
     "start_time": "2025-01-08T10:36:30.534605Z"
    }
   },
   "cell_type": "code",
   "source": [
    "# 依次打印每一列\n",
    "for i in df_data.columns:\n",
    "    print(df_data.iloc[:,i])"
   ],
   "id": "4b9ff246a77afa72",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "0    0.02715\n",
      "1    1.00000\n",
      "2        NaN\n",
      "3    1.00000\n",
      "Name: 0, dtype: float64\n",
      "0    0.912574\n",
      "1    2.000000\n",
      "2    4.000000\n",
      "3    2.000000\n",
      "Name: 1, dtype: float64\n",
      "0    0.070426\n",
      "1         NaN\n",
      "2         NaN\n",
      "3    3.000000\n",
      "Name: 2, dtype: float64\n"
     ]
    }
   ],
   "execution_count": 36
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-01-08T10:33:24.965827Z",
     "start_time": "2025-01-08T10:33:24.959646Z"
    }
   },
   "cell_type": "code",
   "source": [
    "print(df_data)\n",
    "print(\"-\" * 50)\n",
    "\n",
    "print(df_data.iloc[:,0].fillna(0)) # 第0列的NaN用0填充\n",
    "print(\"-\" * 50)"
   ],
   "id": "96a744af2eab970e",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "         0         1         2\n",
      "0  0.02715  0.912574  0.070426\n",
      "1  1.00000  2.000000       NaN\n",
      "2      NaN  4.000000       NaN\n",
      "3  1.00000  2.000000  3.000000\n",
      "--------------------------------------------------\n",
      "0    0.02715\n",
      "1    1.00000\n",
      "2    0.00000\n",
      "3    1.00000\n",
      "Name: 0, dtype: float64\n",
      "--------------------------------------------------\n"
     ]
    }
   ],
   "execution_count": 35
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-01-08T10:37:43.235145Z",
     "start_time": "2025-01-08T10:37:43.230158Z"
    }
   },
   "cell_type": "code",
   "source": [
    "# inplace=True后面会被删除\n",
    "# 即若不进行接收会报错\n",
    "df_data.iloc[:,0].fillna(0, inplace=True) "
   ],
   "id": "2201455918c09797",
   "outputs": [
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "C:\\Users\\32952\\AppData\\Local\\Temp\\ipykernel_24976\\3209618888.py:1: FutureWarning: A value is trying to be set on a copy of a DataFrame or Series through chained assignment using an inplace method.\n",
      "The behavior will change in pandas 3.0. This inplace method will never work because the intermediate object on which we are setting values always behaves as a copy.\n",
      "\n",
      "For example, when doing 'df[col].method(value, inplace=True)', try using 'df.method({col: value}, inplace=True)' or df[col] = df[col].method(value) instead, to perform the operation inplace on the original object.\n",
      "\n",
      "\n",
      "  df_data.iloc[:,0].fillna(0, inplace=True) # 第0列的NaN用前一个值填充\n"
     ]
    }
   ],
   "execution_count": 37
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-01-08T10:39:13.464017Z",
     "start_time": "2025-01-08T10:39:13.459434Z"
    }
   },
   "cell_type": "code",
   "source": [
    "# 第2列的NaN用该列的均值填充\n",
    "df_data.iloc[:,2]=df_data.iloc[:,2].fillna(df_data.iloc[:,2].mean()) \n",
    "print(df_data)"
   ],
   "id": "462edb9747b45a9d",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "         0         1         2\n",
      "0  0.02715  0.912574  0.070426\n",
      "1  1.00000  2.000000  1.535213\n",
      "2  0.00000  4.000000  1.535213\n",
      "3  1.00000  2.000000  3.000000\n"
     ]
    }
   ],
   "execution_count": 38
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 2
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython2",
   "version": "2.7.6"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
